package com.jdwx.opengl;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ProcessBar extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.processbar);
       // saveData();

        Context b = getBaseContext();
        //readData();
        getApplicationContext();


        Uri uri = Uri.parse("content://com.jdwx.opengl.MyContentProvider");
        Uri uri2 = Uri.parse("content://com.jdwx.opengl.MyContentProvider/student");

        @SuppressLint("HandlerLeak")
        Handler handler = new Handler(){
            //handleMessage为处理消息的方法
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("ProcessBar.this","=====我的观察者==我要更改界面" );
            }
        };

        ContentResolver resolver = getContentResolver();
        resolver.registerContentObserver(uri,true, new ContentObserver( handler ){

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
            }

            @Override
            public void onChange(boolean selfChange, Uri uri) {
                super.onChange(selfChange, uri);
                Message message = Message.obtain();
                message.obj = uri;
                handler.sendMessage(message);
                Log.e("ProcessBar.this","=====我的观察者==回调"+uri.toString());
            }
        });


        //内容共享
        Cursor cursor = resolver.query(uri2, null, null, null, null, null);
        while (cursor != null && cursor.moveToNext()) {

            Log.e("ProcessBar","queryData student = " + cursor.getInt(cursor.getColumnIndex("id" ) )
            +";"+cursor.getString(cursor.getColumnIndex("name" )) );
        }






//        Handler h = new Handler();
//        Message m = h.obtainMessage();
//        m.sendToTarget();



//        Db db = new Db(this);
//
//        //插入数据
//        Map map = new HashMap<String,Object>();
//        map.put("name","xiaobai");
//        map.put("gender",1);
//        map.put("number","187677666");
//        map.put("score",90);
//        long id = db.insert("student",map );
//
//        Log.e( "ProcessBar","id="+id);
//
//        //修改数据  把id=1 的 name字段修改为 张三
//
//        db.updateData("update student  set name = \"张三\" where id = 1  ");
//
//
//        //查询
//        Cursor cursor = db.query("student",null,null,0,10);
//        while (cursor != null && cursor.moveToNext()) {
//
//            Log.e("ProcessBar","queryData student = " + cursor.getInt(cursor.getColumnIndex("id" ) )
//            +";"+cursor.getString(cursor.getColumnIndex("name" )) );
//        }
//
//        //方式一
//        //db.delete("student","name=?",new String[]{"xiaobai"});
//        //方式2
//        db.delete2("student","id=25");
//



    }
    private  void saveData(){
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
             out = openFileOutput("datas", Context.MODE_PRIVATE);
             writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write( "我是存储的数sss据" );


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if( null != writer ){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

    private void readData(){

        FileInputStream in = null;
        BufferedReader  reader = null;

        StringBuilder content = new StringBuilder();

        try {
            in = openFileInput("datas");
            reader  = new BufferedReader( new InputStreamReader( in ) ) ;

            String line;

            while ( (line = reader.readLine()) !=null ){
                content.append( line );

            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if( null != reader ){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //((TextView)findViewById(R.id.tv_content)).setText( content );


    }

    private  void provider(){



    }

    @Override //[rɪˈzuːm]
    protected void onResume() {
        super.onResume();
    }
}
